<template>
  <div class="search">
    <van-nav-bar
      title="搜索"
      left-arrow
      :fixed="true"
      @click-left="$router.back()"
    />
    <div class="box">
      <div class="search-h">
        <div class="si">
          <img src="https://shqchapp.com/h5/static/image/zoom.png" alt="" />
          <input type="text" v-model="search" placeholder="请输入搜索值" />
        </div>
        <div class="btn" @click="on">搜索</div>
      </div>
      <div v-if="searchItem.length > 0">
        <div class="record">
          <div class="header">
            <div>历史记录</div>
            <div @click="clear">清除</div>
          </div>
          <div class="content">
            <div v-for="(item, index) in searchItem" :key="index">
              {{ item }}
            </div>
          </div>
        </div>
      </div>
      <div v-if="searchList.length > 0">
        <div class="find">
          <div class="header">
            <div>搜索发现</div>
          </div>
          <div class="content">
            <div v-for="(item, index) in searchList" :key="index">
              {{ item }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getshopconf } from "@/api/home";
export default {
  components: {},
  data() {
    return {
      search: "",
      searchItem: JSON.parse(localStorage.getItem("item")) || [],
      searchList: [],
    };
  },
  created() {
    getshopconf().then((res) => {
      this.searchList = res.data.data.recommend_keys;
    });
  },
  methods: {
    on() {
      if (this.search !== "") {
        this.searchItem.unshift(this.search);
        localStorage.setItem("item", JSON.stringify(this.searchItem));
        this.search = "";
      }
    },
    clear() {
      localStorage.clear("item");
      this.searchItem = [];
    },
  },
};
</script>

<style scoped>
.search {
  bottom: 0;
}
.box {
  position: absolute;
  top: 0.92rem;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgb(248, 248, 248);
}
.search .search-h {
  padding: 0.2rem 0.26rem;
  font-size: 0.25rem;
  color: #888;
  display: flex;
  justify-content: space-between;
  background: #fff;
}
.search .search-h img {
  height: 0.5rem;
  width: 0.5rem;
}
.search .search-h .si {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-indent: 0.15rem;
  background-color: #f1f1f1;
  border-radius: 0.5rem;
  padding: 0.08rem 0.2rem;
}
.search .search-h input {
  flex: 1;
  outline: none;
  border: none;
  background-color: #f1f1f1;
}
.search .search-h .btn {
  width: 1rem;
  text-align: center;
  line-height: 0.72rem;
  background-color: #f1f1f1;
  font-size: 0.28rem;
  color: #333;
  border-radius: 0.06rem;
  margin-left: 0.1rem;
  padding: 0 0.2rem;
}
.search .box .header {
  display: flex;
  justify-content: space-between;
  padding: 0.2rem 0.3rem;
  font-size: 0.28rem;
  color: #333;
}
.box .content {
  display: flex;
  flex-wrap: wrap;
  padding: 0.1rem 0.2rem;
}
.box .content div {
  background-color: #fff;
  color: #888;
  margin-right: 0.2rem;
  margin-bottom: 0.14rem;
  font-size: 0.26rem;
  padding: 0.1rem 0.2rem;
}
.box .record {
  min-height: 2rem;
}
</style>